package com.hr;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.Index;
import org.hibernate.annotations.Table;

import com.espirit.EntityMapping;
import com.espirit.common.VersionAware;
import com.espirit.eap.annotation.Dict;
import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.manager.Department;
import com.espirit.eap.sa.Employee;
import com.espirit.eap.sa.EmployeeAware;
import com.espirit.eap.sa.User;
import com.espirit.finance.core.AcctTerm;
import com.espirit.finance.core.AcctTermCheckAware;
import com.googlecode.cswish.model.Saas;

@Entity
@DomainModel
@Table(indexes={
		@Index(name="idx_accttermemp", columnNames={"acctterm_id","employee_id"} )},appliesTo = "hr_salary")
public class Salary extends EntityMapping implements Serializable, EmployeeAware, VersionAware, AcctTermCheckAware, Saas{
	private static final long serialVersionUID = 408367123930104726L;
	
	public static final String PAY_MODEL = "salary_pay_model";
	
	public static final Integer PAY_MODEL_BANKCOLLECTION = 1;
	public static final Integer PAY_MODEL_BANK = 2;
	public static final Integer PAY_MODEL_CASH = 3;
	
	public static final String  STATE = "salary_state";
	public static final Integer STATE_NORMAL = 0;
	public static final Integer STATE_BANKCOLLECTION = 1;
	public static final Integer STATE_SUSPEND = 2;
	public static final Integer STATE_STOP = 3;
	public static final Integer STATE_SUCCEED = 4;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public AcctTerm getAcctTerm() {
		return acctTerm;
	}
	public void setAcctTerm(AcctTerm acctTerm) {
		this.acctTerm = acctTerm;
	}
	public BigDecimal getAmount() {
		return amount;
	}
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	
	@OneToMany(targetEntity = SalaryItemBase.class ,orphanRemoval = true, fetch=FetchType.LAZY,  cascade = CascadeType.ALL, mappedBy = "parent")
	public List<SalaryItemBase> getChildren() {
		return children;
	}
	public void setChildren(List<SalaryItemBase> children) {
		this.children = children;
	}



	public Date getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public User getCreated() {
		return created;
	}
	public void setCreated(User created) {
		this.created = created;
	}
	
	@Dict(type=PAY_MODEL)
	public Integer getPayModel() {
		return payModel;
	}
	public void setPayModel(Integer payModel) {
		this.payModel = payModel;
	}

	@Dict(type=STATE)
	public Integer getState() {
		return state;
	}
	public void setState(Integer state) {
		this.state = state;
	}
	
	@Transient
	public String getDeptIds() {
		return deptIds;
	}
	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}
	
	@Override
	public boolean check(AcctTerm acctTerm) {
		if (acctTerm != null && acctTerm.getBeginDate() != null && this.acctTerm != null){
			return acctTerm.getName().compareTo(this.acctTerm.getName()) <= 0;
		} else {
			return true;
		}
	}

	private AcctTerm acctTerm;
	private BigDecimal amount;
	private Employee employee;
	private List<SalaryItemBase> children;
	private Date createdOn;
	private Department department;
	private User created;
	private Integer payModel;
	private Integer state;
	private String deptIds;
}
